/*
 * Copyright 2015 Yan Zhenjie
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hengda.museumonline.adapter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hengda.museumonline.R;
import com.hengda.museumonline.entity.TopnewsBean;
import com.hengda.museumonline.ui.NewsContentActivity;

import static com.yolanda.nohttp.NoHttp.getContext;


/**
 * Created on 2016/7/6.
 *
 * @author Yan Zhenjie.
 */
public class MainBannerAdapter extends PagerAdapter {
    private TopnewsBean topnewsBean;
    public static final int[] IMAGES = {R.mipmap.cheese_1, R.mipmap.cheese_2, R.mipmap.cheese_3, R.mipmap.cheese_4, R.mipmap.cheese_5};

    public MainBannerAdapter(TopnewsBean topnewsBean) {
        this.topnewsBean = topnewsBean;
    }

    @Override
    public int getCount() {
        return 2000;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view = View.inflate(getContext(), R.layout.topnews_item, null);
        TextView textView = (TextView) view.findViewById(R.id.title);
        textView.setText(topnewsBean.getData().get(position % topnewsBean.getData().size()).getPost_title());
        ImageView imageView = (ImageView) view.findViewById(R.id.image);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        Glide.with(getContext()).load(topnewsBean.getData().get(position % topnewsBean.getData().size()).getImg()).into(imageView);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putString("object_id", topnewsBean.getData().get(position % topnewsBean.getData().size()).getObject_id());
                Intent intent = new Intent(getContext(), NewsContentActivity.class);
                intent.putExtras(bundle);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getContext().startActivity(intent);
            }
        });
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
